package com.tsing.mapper.impl;

import com.tsing.common.framwork.mapper.CommonDao;
import com.tsing.common.result.PageResult;
import com.tsing.mapper.ChannelMapper;
import com.tsing.pojo.entity.AdChannel;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴阳基
 * @datetime 2023/12/4 9:52
 */
public class ChannelMapperImpl extends CommonDao implements ChannelMapper {

    @Override
    public PageResult<AdChannel> list(int page, int size, Integer status, String name) throws SQLException {
        int offset = (page - 1) * size;
        PageResult<AdChannel> result = new PageResult<>();

        String statusCondition = (status != null) ? "AND status = ?" : "";
        String nameCondition = (name.isEmpty()) ? "" : "AND name = ?";

        String sql = "SELECT * FROM ad_channel WHERE 1=1 " + nameCondition + " " + statusCondition + " LIMIT ?,?";

        List<Object> params = new ArrayList<>();
        if (!name.isEmpty()) {
            params.add(name);
        }
        if (status != null) {
            params.add(status);
        }
        params.add(offset);   // change this line
        params.add(size);     // change this line

        List<AdChannel> data = executeQuery(AdChannel.class, sql, params.toArray());
        result.setData(data);
        result.setTotal(executeCount(
                "SELECT count(?) FROM ad_channel",
                new Object[]{1}));
        result.setSize(size);
        result.setCode(200);
        result.setCurrentPage(page);

        return result;
    }


    @Override
    public int add(AdChannel adChannel) throws SQLException {
        Object[] params = {adChannel.getName(),
                adChannel.getDescription(),
                adChannel.getIsDefault(),
                adChannel.getStatus(),
                adChannel.getOrd(),
                adChannel.getCreatedTime()};

        return executeUpdate("insert into ad_channel (name, description, is_default, status, ord, created_time)\n" +
                "values (?,?,?,?,?,?);", params);
    }

    @Override
    public int update(AdChannel adChannel) throws SQLException {
        StringBuilder sql = new StringBuilder("UPDATE ad_channel SET status = ?, ");
        List<Object> params = new ArrayList<>();

        //测试，会不会

        //如果status为true，则添加1，否则添加0
        params.add(adChannel.getStatus());

        if (adChannel.getName() != null) {
            sql.append("name = ?, ");
            params.add(adChannel.getName());
        }
        if (adChannel.getOrd() != null) {
            sql.append("ord = ?, ");
            params.add(adChannel.getOrd());
        }
        if (adChannel.getDescription() != null) {
            sql.append("description = ?, ");
            params.add(adChannel.getDescription());
        }

        // 移除最后一个逗号并添加 WHERE 语句
        sql.replace(sql.length() - 2, sql.length(), " WHERE id = ?;");
        params.add(adChannel.getId());

        return executeUpdate(sql.toString(), params.toArray());
    }

    @Override
    public int delete(int id) throws SQLException {
        return executeUpdate("delete from ad_channel where id = ?;", new Object[]{id});
    }
}
